package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.PromotionActivityDao;
import com.xbongbong.saas.domain.entity.CouponTempleteEntity;
import com.xbongbong.saas.domain.entity.PromotionActivityEntity;
import com.xbongbong.saas.enums.business.MarketingBaseEnum;
import com.xbongbong.saas.model.PromotionActivityModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

;

/**
 * @author bomfish
 */
@Service("promotionActivityModel")
public class PromotionActivityModelImpl implements PromotionActivityModel {
    @Resource
    private PromotionActivityDao promotionActivityDao;
    @Resource
    private PaasEsModel paasEsModel;

    @Override
    public Long insert(PromotionActivityEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setDel(0);
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Long success = promotionActivityDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PROMOTION_ACTIVITY_MANAGEMENT);
        paasEsModel.insert(insertDTO);
        return success;
    }

    @Override
    public void insertBatch(List<PromotionActivityEntity> list) {
        promotionActivityDao.insertBatch(list);
    }

    @Override
    public List<PromotionActivityEntity> findEntities(Map<String, Object> param) {
        return promotionActivityDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return promotionActivityDao.getEntitiesCount(param);
    }

    @Override
    public void update(PromotionActivityEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_PROMOTION_ACTIVITY_MANAGEMENT);
        paasEsModel.update(insertDTO);
        promotionActivityDao.update(entity);
    }

    @Override
    public PromotionActivityEntity getByKey(Long key, String corpid) {
        return promotionActivityDao.getByKey(key, corpid);
    }

    @Override
    public List<Integer> getDisabledPriority(String corpid, Integer businessType) {
        return promotionActivityDao.getDisabledPriority(corpid, businessType);
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) throws XbbException {
        DeleteDTO deleteDTO = new DeleteDTO();
        deleteDTO.setEsId(corpid+"_"+key);
        deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PROMOTION_ACTIVITY_MANAGEMENT);
        paasEsModel.delete(deleteDTO);
        return promotionActivityDao.deleteByKey(key, corpid);
    }

    @Override
    public Integer getOnlinePromotionCount(String corpid) {
        return promotionActivityDao.getOnlinePromotionCount(corpid);
    }

    @Override
    public void updateStatus(List<PromotionActivityEntity> updateList, String corpid) throws XbbException {
        promotionActivityDao.updateStatus(updateList, corpid);
        List<InsertDTO> updateBatchList = new ArrayList<>();
        for (PromotionActivityEntity entity : updateList) {
            Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(MarketingBaseEnum.STATUS.getAttr(),entity.getStatus());
            map.put(StringConstant.JSON_DATA, object);
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PROMOTION_ACTIVITY_MANAGEMENT);
            updateBatchList.add(insertDTO);
        }
        paasEsModel.updateBatch(updateBatchList, WriteRequest.RefreshPolicy.NONE, false);
    }

}
